package com.homeinns.dao;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import com.homeinns.model.FrequencePerson;

public class FrequencePersonDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	/*
	SELECT TOP 1000 [id]
      ,[name]
      ,[phone]
      ,[userId]
      ,[createtime]
  FROM [CRS].[dbo].[FrequencePerson]
	*/
	//userid should be indexed
	public List<FrequencePerson> getFrequencePersons(String userId){
		String sql = "select * from FrequencePerson where userId=?";
		Object[] args = new Object[] { userId };
		List<FrequencePerson> persons = jdbcTemplate.query(sql, args,new FrequencePersonMapper());
		return persons;
	}
	
	public void addFrequencePerson(String name,String phone,String userId){
		String sql = "insert into FrequencePerson(name,phone,userId) values(?,?,?)";
		Object[] args = new Object[]{name,phone,userId};
		jdbcTemplate.update(sql,args);
	}
	
	public void delFrequencePerson(String id){
		String sql = "delete from FrequencePerson where id=?";
		Object[] args = new Object[]{id};
		jdbcTemplate.update(sql,args);
	}
	
	class FrequencePersonMapper implements RowMapper<FrequencePerson> {

		@Override
		public FrequencePerson mapRow(ResultSet rs, int rowNum) throws SQLException {
			FrequencePerson cc = new FrequencePerson();
			cc.setId(rs.getString("id"));
			cc.setName(rs.getString("name"));
			cc.setPhone(rs.getString("phone"));
			cc.setUserId(rs.getString("userId"));
			return cc;
		}

	}
}
